package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.filter.UserFilter;
import com.imooc.mall.service.CartService;
import com.imooc.mall.vo.CartVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车controller
 */
@RequestMapping("/cart")
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("购物车列表功能")
    @GetMapping("/list")
    public ApiRestResponse list(){
        //内部获取用户id，防止越权
        List<CartVO> list = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(list);


    }

    @ApiOperation("给购物车添加商品功能")
    @PostMapping("/add")
    @ResponseBody
    public ApiRestResponse add(@RequestParam Integer productId,
                               @RequestParam Integer count){

        List<CartVO> add = cartService.add(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(add);
    }

    /**
     * 购物车更新功能
     */
    @ApiOperation("更新购物车功能")
    @PostMapping("/update")
    public ApiRestResponse update(@RequestParam Integer productId,
                                  @RequestParam Integer count){
        List<CartVO> update = cartService.update(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(update);
    }


    /**
     * 删除车更新功能
     */
    @ApiOperation("删除购物车功能")
    @PostMapping("/delete")
    public ApiRestResponse delete(@RequestParam Integer productId
                                 ){
        List<CartVO> delete = cartService.delete(productId, UserFilter.currentUser.getId());
        return ApiRestResponse.success(delete);
    }

    /**
     * 选择或不选择购物车的某一个商品
     */
    @PostMapping("/select")
    @ApiOperation("选择或不选择购物车的某一件商品")
    public ApiRestResponse select(@RequestParam Integer productId,
                                  @RequestParam Integer selected){

        List<CartVO> update = cartService.selectOrNot(UserFilter.currentUser.getId(), productId, selected);
        return ApiRestResponse.success(update);
    }

    /**
     * 选择或不选择购物车的某一个商品
     */
    @PostMapping("/selectAll")
    @ApiOperation("选择或不选择购物车的全部件商品")
    public ApiRestResponse selectAll(@RequestParam Integer selected){

        List<CartVO> cartVOS = cartService.selctAllOrNot(UserFilter.currentUser.getId(), selected);
        return ApiRestResponse.success(cartVOS);

    }
}
